/**
 * @Description:
 * @author 若琳
 * @date 2021/8/16
*/



const {MYSQL}  = require("../../../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");
const Pager_DB = require("../../../../../../../../ruoling-common/src/com/ruoling/common/table/Pager_DB.js");

module.exports = class DictService{


    /**
     * 字典类型列表Service
     * @param limit
     * @param page
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictTypeListService(limit,page,sys_dict_name,callback){
        try{
            let pagerdb = await new Pager_DB(limit,page).table("rl_sys_dict_type")
                .and({"key":"sys_dict_name","value":sys_dict_name})
                .select();
            callback(null,pagerdb);
            return;
        }catch (e){
            callback(e);
            return;
        }
    };


    /**
     * 字典类型添加Service
     * @param sys_dict_name
     * @param sys_dict_datatype
     * @param sys_dict_remark
     * @param sys_dict_status
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictTypeAddService(sys_dict_name,sys_dict_datatype,sys_dict_remark,sys_dict_status,callback){
        let startTrans = null;
        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            //获取sys_dict_datatype是否有出现
            let dicttype_obj={
                sql:"select * from rl_sys_dict_type where sys_dict_datatype=\""+sys_dict_datatype+"\" and Is_delete = 1"
            }
            let dacttype_data = await startTrans.query(dicttype_obj.sql,dicttype_obj.param);
            if(dacttype_data.length > 0){
                await startTrans.commit();
                callback(null,{code:1,msg:"字典类型已存在"});
                return ;
            }
            let role_obj = {
                sql:"insert into rl_sys_dict_type(sys_dict_name,sys_dict_datatype,sys_dict_remark,sys_dict_status,Create_time)values(?,?,?,?,?)",
                param:[sys_dict_name,sys_dict_datatype,sys_dict_remark,sys_dict_status,new Date()]
            };
            await startTrans.query(role_obj.sql,role_obj.param);
            await startTrans.commit();
            callback(null,{code:0,msg:"字典类型添加成功"});
            return;
        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    };


    /**
     * 字典类型编辑Service
     * @param sys_dict_name
     * @param sys_dict_datatype
     * @param sys_dict_remark
     * @param sys_dict_status
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictTypeEditService(sys_dict_name,sys_dict_remark,sys_dict_status,Id,sys_dict_datatype,callback){
        let startTrans = null;
        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            //获取sys_dict_datatype是否有出现,不包含自身
            let dicttype_obj={
                sql:"select * from rl_sys_dict_type where sys_dict_datatype=\""+sys_dict_datatype+"\" and Is_delete = 1 and ID <> "+Id+""
            }
            let dacttype_data = await startTrans.query(dicttype_obj.sql,dicttype_obj.param);
            if(dacttype_data.length > 0){
                await startTrans.commit();
                callback(null,{code:1,msg:"字典类型已存在"});
                return ;
            }

            let role_obj = {
                sql:"update rl_sys_dict_type set sys_dict_name=\""+sys_dict_name+"\",sys_dict_remark=\""+sys_dict_remark+"\",sys_dict_status="+sys_dict_status+" WHERE ID="+Id+"",
            };
            await startTrans.query(role_obj.sql);
            await startTrans.commit();
            callback(null,{code:0,msg:"字典类型修改成功"});
            return;
        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    };

    static async DictTypeById(Id,callback){
        let connect = null;
        try {

            connect = await MYSQL().connect();
            let data = await connect.connect_query_close("select * from rl_sys_dict_type where ID = "+Id+"")
            callback(null,data[0]);
            return;
        }catch (e){
            if(connect != null){
                connect.close();
            }
            callback(e);
            return;
        }
    };


    /**
     * 字典数据列表Service
     * @param limit
     * @param page
     * @param sys_dict_type
     * @param sys_dict_label
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictDataListService(limit,page,sys_dict_type,sys_dict_label,callback){
        try{
            let pagerdb = await new Pager_DB(limit,page).table("rl_sys_dict_data")
                .and({"key":"sys_dict_type","value":sys_dict_type})
                .and({"key":"sys_dict_label","value":sys_dict_label})
                .select();
            callback(null,pagerdb);
            return;
        }catch (e){
            callback(e);
            return;
        }
    };

    /**
     * 字典数据添加
     * @param sys_dict_state
     * @param sys_dict_sort
     * @param sys_dict_label
     * @param sys_dict_value
     * @param sys_dict_type
     * @param sys_dict_remark
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictDataddService(sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,callback){
        let startTrans = null;
        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            let obj = {
                sql:"insert into rl_sys_dict_data(sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,Create_time,sys_dict_is_default)values(?,?,?,?,?,?,?,?)",
                param:[sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,new Date(),"N"]
            };
            await startTrans.query(obj.sql,obj.param);
            await startTrans.commit();
            callback(null,{code:0,msg:"字典数据添加成功"});
            return;
        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    };

    /**
     * 字典数据编辑
     * @param sys_dict_state
     * @param sys_dict_sort
     * @param sys_dict_label
     * @param sys_dict_value
     * @param sys_dict_type
     * @param sys_dict_remark
     * @param Id
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictDataEditService(sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,Id,callback) {
        let startTrans = null;
        try {
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            let obj = {
                sql: "update rl_sys_dict_data set sys_dict_state = " + sys_dict_state + ",sys_dict_sort=" + sys_dict_sort + ",sys_dict_label=\"" + sys_dict_label + "\",sys_dict_value=\"" + sys_dict_value + "\",sys_dict_type=\"" + sys_dict_type + "\",sys_dict_remark=\"" + sys_dict_remark + "\" WHERE ID=" + Id + "",
            };
            await startTrans.query(obj.sql);
            await startTrans.commit();
            callback(null, {code: 0, msg: "字典数据编辑成功"});
            return;
        } catch (e) {
            await startTrans.rollback();
            callback(e);
            return;
        }
    };

    /**
     * 字典类型删除
     * @param Id
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async DictTypeDeleteService(Id,callback){
        let startTrans = null;
        try {

            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            let obj = {
                sql: "select * from rl_sys_dict_type WHERE ID = "+Id+" AND Is_delete = 1",
            };
            let data = await startTrans.query(obj.sql);
            let type_obj ={
                sql:"update rl_sys_dict_type set Is_delete = 0 where ID = "+Id+""
            };
            let data_obj = {
                sql:"update rl_sys_dict_data set Is_delete = 0 where sys_dict_type = \""+data[0].sys_dict_datatype+"\""
            };

            await startTrans.query(data_obj.sql);
            await startTrans.query(type_obj.sql);
            await startTrans.commit();
            callback(null, {code: 0, msg: "字典数据删除成功"});
            return;
        } catch (e) {
            await startTrans.rollback();
            callback(e);
            return;
        }
    };

    static async DictDataById(Id,callback){
        let connect = null;
        try {

            connect = await MYSQL().connect();
            let data = await connect.connect_query_close("select * from rl_sys_dict_data where ID = "+Id+"")
            callback(null,data[0]);
            return;
        }catch (e){
            if(connect != null){
                connect.close();
            }
            callback(e);
            return;
        }
    }

}